<?php

namespace App\Http\Controllers\Home;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use DB;

class DetailController extends Controller
{
    //展示
    public function show(Request $request,$id)
    {	
    	$title = "商品详情展示";
         $conf = DB::table('bro_config') -> first();
    	//会话跟踪
        $user = $request->session()->get('user');
         //获取所有大类别
        $type = DB::table('bro_cate') -> where('pid',1) -> get();
        // $id = $type[0] -> id;
        //获取各类别的小类别
        foreach($type as $v)
        {
            $cid  = $v -> id;
            // $id = $type[$i] -> id;
            // dd($v);
            $typechild = DB::table('bro_cate') -> where('pid',$cid) -> get();
            // dd($typechild);
            $typechilds[] = $typechild;
        }
        //友情链接
        $link = DB::table('bro_link') -> orderBy('order', 'asc') -> get();
        // dd($link);
        //获取服装的品牌
        $brands = DB::table('bro_brand') -> where('status','=',0) -> get();
        //获取推荐商品
        $tjgoods = DB::table('bro_goods') -> where('tuijian',1) -> get();
        //遍历出商品ID,查询评价
        // foreach($tjgoods as $val)
        // {
        // 	$comment = DB::table('bro_comment') -> where('goods_id',$val -> id) -> get();
        //     // dd($comment);
        // 	$val -> num = count($comment);
        // }
            $comment = DB::table('bro_comment') -> where('goods_id',$id) -> get();
            // dd($comment);

        //=====获取商品的信息
        $goods = DB::table('bro_goods') -> where('id',$id) -> first();
        //====处理商品的大图
        $pic = get_object_vars($goods)['describe'];
        // dd($pic);
        preg_match_all("/\/uploads\/.*?\.jpg/",$pic,$match);


       	$cate_id = $goods -> category_id;
       	$cate = DB::table('bro_cate') -> where('id',$cate_id) ->first();//查本商品的类别
       	
       	$precate = DB::table('bro_cate') -> where('id',$cate -> pid) -> first();//查本商品的上一级类别
       	$catename ['cate'][]  = $precate -> id;
       	$catename ['cate'][]  = $precate -> catename;
       	$catename ['precate'][] = $cate -> id;
       	$catename ['precate'][] = $cate -> catename;
       	
       	$goods -> color = explode(',',$goods -> color);
       	$goods -> size = explode(',',$goods -> size);
       	// dd($goods);

        // dd($link);
        // return view('welcome') ->with(['title' => $title,'article' => $article,'brands' => $brands,'tjgoods' => $tjgoods,'lgoods' => $lgoods,'brandgoods' => $brandgoods,'low' => $low,'link' => $link]);
    	return view('home.detail.detail')-> with(['title' => $title,'type' => $type,'typechilds' => $typechilds,'user' => $user,'brands' => $brands,'link' => $link,'tjgoods' => $tjgoods,'catename' => $catename,'goods' => $goods,'comment' => $comment,'match' => $match[0],'conf' => $conf]);
    }

    //商品关注
    public function collect(Request $request,$id)
    {

    	//判断用户是否登录  	
    	 $user = $request->session()->get('user');
    	 if(!empty($user))
    	 {
    	 	$data['uid'] = $uid = $user -> id;
    	 	$data['collecttime'] = time();
    	 	$data['goods_id'] = $id;

    	 	//添加关注商品
    	 	
    	 	$res = DB::table('bro_collect') -> insert($data);
    	 	if($res)
    	 	{
    	 		echo '<script>alert("关注成功");window.location.href="/detail/'.$id.'";</script>';
    	 	}
    	 }else{
    	 	    echo '<script>alert("请先登录!");window.location.href="/login";</script>';
    	 }
    	
    }

    //添加购物车
    public function insert(Request $request)
    {
        $user = $request -> session() -> get('user');
        if(!empty($user))
        {
            $data = $request -> except('_token','goods_name');

            $data['gwtime'] = time();//=====生成购物时间

            $data['uid'] = $user -> id; //=====取用户ID
            // dd($data);

            $res = DB::table('bro_cart') -> insert($data);

            if($res)
            {
                echo '<script>alert("加入购物车成功");window.location.href="/detail/'.$data['goods_id'].'";</script>';
            }else
            {
                echo '<script>alert("加入购物车失败");window.location.href="/detail/'.$data['goods_id'].'";</script>';
            }
        }else{
            // return redirect('/login');
            echo '<script>alert("请先登录!");window.location.href="/login";</script>';

        }
    }
}
